package com.jingtai.platform.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.entity.admin.PlatformRole;
import com.jingtai.core.mapper.admin.PlatformRoleMapper;
import com.jingtai.platform.service.admin.PlatformRoleService;
import com.jingtai.platform.util.ResultUtil;
import com.jingtai.platform.util.UrlToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 *  impl
 *
 * @author sunlaiqian
 * @date 2020-12-04 19:30:36
 */
@Service
public class PlatformRoleServiceImpl extends ServiceImpl<PlatformRoleMapper, PlatformRole> implements PlatformRoleService {

    @Autowired
    private PlatformRoleService platformRoleService;

    @Override
    public ResultUtil PlatformRolelist(Integer page, Integer limit, String conditions){
        QueryWrapper<PlatformRole> wrapper = new QueryWrapper<>();
        Map<String, String> map = UrlToMap.paramToMap(conditions);
        for(Map.Entry<String, String> entry : map.entrySet()){
            wrapper.like(entry.getKey(), entry.getValue());
        }
        wrapper.eq("delete_flag", DeleteFlagEnum.NOT_DELETE.getCode());
        return new ResultUtil(ResultMapInfo.GETSUCCESS,platformRoleService.page(new Page<>(page,limit),wrapper));
    }

    @Override
    public ResultUtil addPlatformRole(PlatformRole platformRole) {
        boolean save = platformRoleService.save(platformRole);
        return new ResultUtil(save ? ResultMapInfo.ADDSUCCESS:ResultMapInfo.ADDFAIL);
    }

    @Override
    public ResultUtil getPlatformRoleById(Long id) {
        PlatformRole platformRole = platformRoleService.getById(id);
        return platformRole == null ?  new ResultUtil(ResultMapInfo.GETFAIL) : new ResultUtil(ResultMapInfo.GETSUCCESS,platformRole);
    }

    @Override
    public ResultUtil delPlatformRoleById(Long id) {
        PlatformRole platformRole = new PlatformRole();
        platformRole.setId(id);
        platformRole.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        boolean delState = platformRoleService.updateById(platformRole);
        return delState ?  new ResultUtil(ResultMapInfo.DELETESUCCESS) : new ResultUtil(ResultMapInfo.DELETEFAIL);
    }

    @Override
    public ResultUtil selectAllRole() {
        List<PlatformRole> list = platformRoleService.list(Wrappers.lambdaQuery(PlatformRole.class).eq(PlatformRole::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        return new ResultUtil(ResultMapInfo.GETSUCCESS,list);
    }

    @Override
    public ResultUtil editPlatformRole(PlatformRole platformRole) {
        boolean editState = platformRoleService.updateById(platformRole);
        return editState ?  new ResultUtil(ResultMapInfo.EDITSUCCESS) : new ResultUtil(ResultMapInfo.EDITFAIL);
    }

}
